﻿;(function() {
    'use strict';
    angular.module('controllers.workOrderData.auditData')
        .controller('NCRateController', NCRateController);

    function NCRateController($scope, NCRateService, auditDataService, $timeout, COREDATA, CoreLoading, workOrderDataService) {
        $scope.flag = {
            state: true,
            show: 'view',
            arrow: false
        };
        //导出路径
        $scope.formPrefix = COREDATA.FORMPREFIX + 'workOrderData/NCPercentage/export';
        //页面查询项绑定对象
        var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth();
        $scope.params = {
            startTime: new Date(year, month - 1, 1),
            endTime: new Date(year, month, 0),
            statisticsType: 't2001',
            timeType: '1'
        };
        $scope.initQuery = function() {
            auditDataService.queryStatisticsType({
                code: '2'
            }, function(res) {
                $scope.statisticsTypes = res.data;
                $scope.search();
            });
        };
        var myEcharts = echarts.init(document.getElementById('doc'));
        //查询
        $scope.search = function() {
            $scope.flag.state = !$scope.flag.state;
            var xAxisData = [];
            var seriesData = [];
            var option = {};
            $scope.NCNumTh = $scope.params.statisticsType === 't2001' ? '审计NC数' : '严重NC数';
            $scope.totalNumTh = $scope.params.statisticsType === 't2001' ? '审计工单总数' : 'NC总数';
            $scope.NCPercentageTh = $scope.params.statisticsType === 't2001' ? 'NC比例' : '严重NC比例';
            if (!$scope.flag.state) {
                $scope.send = NCRateService.fmtSendData($scope.params);
                var timeTitle = auditDataService.fmtDateTitle($scope.send);
                //标题:时间+期间各类型工单+NC比例;
                $scope.name = auditDataService.getStatisticsTypeName($scope.params.statisticsType, $scope.statisticsTypes);
                $scope.send.title = timeTitle + '各类型工单' + $scope.name;
                CoreLoading.setState(true);

                NCRateService.query($scope.send, function(res) {
                    if (res.state === '10000' && res.data.length >= 1) {
                        myEcharts.clear();
                        angular.forEach(res.data, function(item) {
                            xAxisData.push(item.workOrderType);
                            seriesData.push(item.ncPercentage.substring(0, item.ncPercentage.length - 1));
                        });
                        $scope.items = res.data;
                        $scope.title = $scope.send.title;
                        $scope.item = workOrderDataService.getArrayData(res.data);
                        if ($scope.params.statisticsType === 't2001') {
                            option = workOrderDataService.getEchartsData(xAxisData, seriesData, $scope.title, $scope.name);
                            option.yAxis[0].axisLabel = {
                                formatter: '{value} %'
                            };

                        } else {
                            option = NCRateService.getEchartsData($scope.items, $scope.title);

                        }
                        option.tooltip = {
                            formatter: function(params) {
                                return params.seriesName + ' <br/>' + params.name + ': ' + params.value + '%';
                            }
                        };
                        myEcharts.setOption(option);
                        $scope.outData = workOrderDataService.exportView(myEcharts, $scope.send);
                        $scope.flag.arrow = true;
                        $scope.flag.show = 'view';
                    } else if (res.data.length === 0) {
                        workOrderDataService.queryNoDataOrError($scope.flag, '查询无数据');
                    } else {
                        workOrderDataService.queryNoDataOrError($scope.flag, '查询错误');
                    }
                    CoreLoading.setState(false);
                });
            }
        };

        //监听表单,当表单有错时,禁止查询
        $scope.$watch('myForm.$invalid', function(newValue) {
            newValue ? $scope.flag.arrow = false : $scope.flag.arrow = true;
        });
    }
    NCRateController.$inject = ['$scope', 'NCRateService', 'auditDataService', '$timeout', 'COREDATA', 'CoreLoading', 'workOrderDataService'];
})();
